package com.huibao.hulibao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.huibao.hulibao.R;
import com.huibao.hulibao.adapter.ZhangHaoLvAdapter;
import com.huibao.hulibao.entity.AllLoginEntity;
import com.huibao.hulibao.entity.LoginEntity;
import com.huibao.hulibao.utils.PreUtils;
import com.huibao.hulibao.utils.T;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/8/31.
 */
public class ChangeLoginNameActivity extends BaseActivity {

    @BindView(R.id.tv_delete)
    TextView tv_delete;
    @BindView(R.id.more_lv_zhanghao)
    ListView more_lv_zhanghao;

    @OnClick({R.id.tv_back, R.id.iv_back})
    public void back() {
        onBackPressed();
    }


    private ZhangHaoLvAdapter zhangHaoLvAdapter;

    @OnClick(R.id.tv_delete)
    public void deleteAll() {
        if (le != null) {
            final AllLoginEntity ale = new AllLoginEntity();
            ArrayList<LoginEntity> al = new ArrayList<LoginEntity>();
            al.add(le);
            ale.setAll_login(al);
            PreUtils.saveObject(getApplicationContext(), "AllLoginEntity", ale);
            zhangHaoLvAdapter = new ZhangHaoLvAdapter(ale.getAll_login(), getApplicationContext(), le.Message.LoginName);
            more_lv_zhanghao.setAdapter(zhangHaoLvAdapter);
            // 快捷登陆的点击入口
            more_lv_zhanghao.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (!ale.getAll_login().get(position).Message.LoginName.equals(le.Message.LoginName)) {
                        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                        intent.putExtra("loginName", ale.getAll_login().get(position).Message.LoginName);
                        startActivity(intent);
                    }
                }
            });
            T.show(getApplicationContext(), getString(R.string.deleteotheraccountsuccess), 0);
        }
    }


    LoginEntity le;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_login_name);
        ButterKnife.bind(this);
        // 判断用户是否登录
        le = (LoginEntity) PreUtils.getObject(getApplicationContext(), "LoginEntity");
        if (le == null) {
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            final AllLoginEntity ale = (AllLoginEntity) PreUtils.getObject(getApplicationContext(), "AllLoginEntity");
            if (ale != null && ale.getAll_login() != null) {
                if (ale.getAll_login().size() > 1) {
                    tv_delete.setVisibility(View.VISIBLE);
                } else {
                    tv_delete.setVisibility(View.INVISIBLE);
                }
                // 测试数据
//                al_datas = new ArrayList<LoginEntity>();
//                al_datas.add(le);
                zhangHaoLvAdapter = new ZhangHaoLvAdapter(ale.getAll_login(), getApplicationContext(), le.Message.LoginName);
                more_lv_zhanghao.setAdapter(zhangHaoLvAdapter);
                // 快捷登陆的点击入口
                more_lv_zhanghao.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (!ale.getAll_login().get(position).Message.LoginName.equals(le.Message.LoginName)) {
                            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                            intent.putExtra("loginName", ale.getAll_login().get(position).Message.LoginName);
                            startActivity(intent);
                        }
                    }
                });
            }
        }
    }
}
